﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using Pampa.CRUD.Configuration;
using Pampa.CRUD.Security;
using Pampa.CRUD.UI.CustomControls;
using Pampa.WebControls;

namespace Pampa.MVC.Fluent.Crud
{
    public class EntityEditBuilder : IHtmlString 
    {
        private string _clientId;
        private object _entity;
        private string _onSuccess;
        private bool _inlineErrors;

        private bool _includeSaveButton;
        private string _saveButtonText;

        private bool _includeCancelButton;
        private string _cancelButtonText;
        private string _onCancel;

        private EntityConfiguration _config;
        private ISecurityManager _securityManager;

        private bool _includeClientLibraries;

        public EntityEditBuilder(object entity)
        {
            _entity = entity;
            _inlineErrors = true;
            _includeClientLibraries = true;
        }

        public EntityEditBuilder ClientId(string clientId)
        {
            _clientId = clientId;
            return this;
        }

        public EntityEditBuilder IncludeSaveButton(string text)
        {
            _includeSaveButton = true;
            _saveButtonText = text;
            return this;
        }

        public EntityEditBuilder IncludeCancelButton(string text, string onCancel)
        {
            _includeCancelButton = true;
            _cancelButtonText = text;
            _onCancel = onCancel;

            return this;
        }

        public EntityEditBuilder OnClientSuccess(string onSuccess)
        {
            _onSuccess = onSuccess;
            return this;
        }

        public EntityEditBuilder InlineErrors(bool value)
        {
            _inlineErrors = value;
            return this;
        }

        public EntityEditBuilder Config(string entityName, ISecurityManager securityManager = null)
        {
            _config = ConfigurationManager.GetEditConfiguration(entityName);
            _securityManager = securityManager;
            return this;
        }
        
        public EntityEditBuilder Config(EntityConfig config)
        {
            config.Init();

            _config = config.Configuration;
            _securityManager = config.SecurityManager;
            return this;
        }

        public void AutoConfig()
        {
            // TODO automapping based on AR attributes
        }

        /// <summary>
        /// TODO. parche medio feo para popups con pampa. hacer algo mejor.
        /// </summary>
        public EntityEditBuilder NotIncludeClientLibraries()
        {
            _includeClientLibraries = false;
            return this;
        }

        public string ToHtmlString()
        {
            if (_entity == null)
                throw new Exception("Entity is null.");

            var entityEdit = new EntityEdit();
            entityEdit.ID = _clientId;
            entityEdit.CssClass = "entityEdit";
            entityEdit.Entity = _entity;
            entityEdit.InlineErrors = _inlineErrors;
            entityEdit.Configuration = _config;
            entityEdit.SecurityManager = _securityManager;

            var sb = new StringBuilder();

            if (_includeClientLibraries)
                sb.Append(EntityEdit.Includes());

            sb.Append(entityEdit.Render());

            if (_includeSaveButton)
            {
                var saveButton = new EntityEditSaveButton();
                saveButton.EntityEditID = entityEdit.ID;
                saveButton.CssClass = "saveButton button";
                saveButton.Text = _saveButtonText;

                if (_onSuccess != null)
                    saveButton.OnSuccess = _onSuccess;

                sb.Append(saveButton.Render());
            }

            if (_includeCancelButton)
            {
                var cancelButton = new ClientButton();
                cancelButton.MethodName = _onCancel;
                cancelButton.CssClass = "button";
                cancelButton.Text = _cancelButtonText;
                sb.Append(cancelButton.Render());
            }

            return sb.ToString();
        }

        public override string ToString()
        {
            return ToHtmlString();
        }
    }
}
